package com.car.serve.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.commons.core.model.Result;
import com.car.serve.Param.addCarTypeParam;
import com.car.serve.dto.CarTypeDto;
import com.car.serve.entity.CarType;
import com.car.serve.entity.ServeType;
import com.car.serve.exception.ServeException;
import com.car.serve.form.PageCarTypeForm;
import com.car.serve.form.addCarTypeForm;
import com.car.serve.service.CarTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 董旭
 * @since 2024-08-28
 */
@RestController
@RequestMapping("/carType")
@Api(tags = "车辆类型信息")
public class CarTypeController {

    @Resource
    private CarTypeService carTypeService;

    @GetMapping("/ByIdType.ty")
    @ApiOperation(value = "根据ID查询汽车类型")
    public Result<CarTypeDto> ByIdType(Integer carTypeId)throws Exception{
        CarType carType = carTypeService.getById(carTypeId);
        if (carType==null)throw new ServeException(3000,"车辆类型不存在");
        return Result.getResult(CarTypeDto.class).setMsg("根据ID查询汽车类型成功").setData(BeanUtil.toBean(carType,CarTypeDto.class));
    }

    @PostMapping("/addCarType")
    @ApiOperation(value = "新增车辆类型")
    public Result addCarType(@RequestBody addCarTypeForm form)throws Exception{
        carTypeService.addCarType(BeanUtil.toBean(form, addCarTypeParam.class));
        return Result.getResult().setMsg("新增车辆类型成功");
    }

    @PostMapping("/findCarTypePage")
    @ApiOperation(value = "分页查询")
    public Result findCarTypePage(@RequestBody PageCarTypeForm form)throws Exception{
        Page<CarType> carTypePage = carTypeService.findCarTypePage(form);
        return Result.getResult().setMsg("分页查询成功").setData(carTypePage);
    }


    @GetMapping("/getMyCarType.pt")
    @ApiOperation(value = "查询车辆类型")
    public Result getMyCarType()throws Exception{
        List<CarType> myCarType = carTypeService.getMyCarType();
        return Result.getResult().setMsg("查询类别成功").setData(myCarType);
    }
}

